package com.yfht.framework.base.service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yfht.framework.base.dao.IMultiDAO;
import com.yfht.framework.base.page.HiberanteQueryBean;
import com.yfht.framework.base.page.IQuery;
import com.yfht.framework.base.page.QueryResult;


/**
 * 平台基本Service
 * 
 * @author yuan
 * 
 */
@Component("baseService")
@SuppressWarnings("unchecked")
public class BaseService {
	protected static final Log logger = LogFactory.getLog(BaseService.class);
	
	/*
	 * 注入的多元化的Dao类
	 */
	@Resource
	private IMultiDAO multiDao;
	private int i = 0;

	/** *************************************************数据操作Hibernate&JDBC&Ibatis*********************************************************** */
	/**
	 * 对数据库的增删改操作
	 * 
	 * @param object
	 * @throws DataAccessException
	 */
	public void save_Hibernate_Object(Object model) {
//		i++;
//		if (i > 100) {// 限制功能,试用版只能对数据库进行插入数据100次操作
//			// throw new ServiceException(L11l11l11l1111.WARNING);
//		}
		multiDao.save_Hibernate_Object(model);
		
	}

	public void delete_Hibernate_Object(Object model) throws DataAccessException {
		multiDao.delete_Hibernate_Object(model);
	}

	public void deleteByID_Hibernate_Object(Class cls, Serializable id) throws DataAccessException {
		multiDao.deleteByID_Hibernate_Object(cls, id);
	}

	public void delete_JDBC(final String sql) throws DataAccessException {
		multiDao.delete_JDBC(sql);
	}

	public void bulkUpdate(String queryString) throws DataAccessException {
		multiDao.bulkUpdate(queryString);
	}

	public void bulkUpdate(String queryString, Object value) throws DataAccessException {
		multiDao.bulkUpdate(queryString, value);
	}

	public void bulkUpdate(final String queryString, final Object[] values) throws DataAccessException {
		multiDao.bulkUpdate(queryString, values);
	}

	public void update_Hibernate_Object(Object model) throws DataAccessException {
		multiDao.update_Hibernate_Object(model);
	}

	public void update_JDBC(final String sql) throws DataAccessException {
		multiDao.update_JDBC(sql);
	}

//	public void update_iBatis(final String queryID, final Object params) throws DataAccessException {
//		multiDao.update_iBatis(queryID, params);
//	}

	// 新增加方法，批量删除
	@Transactional(propagation = Propagation.MANDATORY)
	public void deleteByID_Hibernate_Object(Class cls, Serializable[] ids) {
		if (ids != null && ids.length > 0) {
			for (Serializable id : ids) {
				if (id != null) {
					multiDao.deleteByID_Hibernate_Object(cls, id);
				}
			}
		}
	}

	/** *************************************************数据查询Hibernate*********************************************************** */

	public Object findByID_Hibernate_Load(Class cls, Serializable id) {
		return multiDao.findByID_Hibernate_Load(cls, id);
	}

	public Object findByID_Hibernate_Get(Class cls, Serializable id) {
		return multiDao.findByID_Hibernate_Get(cls, id);
	}

	public Object findByID_Hibernate_NamedQuery(String queryID, Serializable id) {
		return multiDao.findByID_Hibernate_NamedQuery(queryID, id);
	}

	public List findAll_Hibernate_Object(Class cls) {
		return multiDao.findAll_Hibernate_Object(cls);
	}

	/**
	 * Hibernate对象查询
	 */
	public List findByExample_Hibernate(Object exampleEntity) {
		return multiDao.findByExample_Hibernate(exampleEntity);
	}
	/**
	 * Hibernate对象分页查询
	 */
	public List findByExample_Hibernate(Object exampleEntity, int firstResult, int maxResults) {
		return multiDao.findByExample_Hibernate(exampleEntity, firstResult, maxResults);
	}

	/**
	 * Hibernate对象分页查询 支持查询条件
	 */
	public List findByHQL_Hibernate(String hql, int firstResult, int maxResults) {
		return multiDao.findByHQL_Hibernate(hql, firstResult, maxResults);
	}

	/**
	 * Hibernate对象分页查询 支持查询条件
	 */
	public List findByHQL_Hibernate(String hql, int firstResult, int maxResults, final Object... params) {
		return multiDao.findByHQL_Hibernate(hql, firstResult, maxResults, params);
	}

	/** *************************************************数据查询JDBC*********************************************************** */
	public List findAll_JDBC(String sql) {
		return multiDao.findAll_JDBC(sql);
	}

	/** *************************************************数据查询iBatis*********************************************************** */
//	public List find_iBatis(String queryID) {
//		return multiDao.findAll_iBatis(queryID);
//	}
//
//	public List find_iBatis(String queryName, Object para) {
//		return multiDao.find_iBatis(queryName, para);
//	}
//
//	public List find_iBatis(final String queryID, final Object para, final int offset, final int size) {
//		return multiDao.find_iBatis(queryID, para, offset, size);
//

	/**
	 * 描述：hibernate分页查询，针对HQL语句
	 * 
	 * @param sqlCountName
	 * @param sqlListName
	 * @param query
	 * @return
	 */
	public QueryResult queryForPageResult_Hibernate(String listHQL, HiberanteQueryBean query) {
		QueryResult result = null;
			// 取得分页信息
		int offset = query.getOffset();
		int size = query.getPageSize();
		// 取得查询条件
		String countHQL = multiDao.formateCountHQL(listHQL);
		List countlist = multiDao.findByHQL_Hibernate(countHQL);
		Integer count = 0;
		if (countlist != null && countlist.size() > 0) {
			count = Integer.valueOf(countlist.get(0).toString());
		}
		int startIndex = query.getPageSize()*(query.getPageNum()-1);
		List list = multiDao.findByHQL_Hibernate(listHQL, startIndex, query.getPageNum(),query.getHqlParams());

		result = new QueryResult(list, count == null ? 0 : count.intValue());
		list = null;
		countlist = null;
		return result;
	}

	/**
	 * 取得表中前n条记录
	 */
	public List queryTopNRecord(Class cls, int n, String orderFieldName) {
		String listHql = " from " + cls.getSimpleName();
		if (orderFieldName != null) {
			listHql += " order by " + orderFieldName;
		}
		List list = multiDao.findByExample_Hibernate(listHql, 0, n);
		return list;
	}
}
